package top.catchcat.blogs.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.springframework.stereotype.Component;
import top.catchcat.blogs.dao.AccessTokenDAO;
import top.catchcat.blogs.dao.userIfo.GiteeUser;

import java.io.IOException;

@Component
public class GiteeProvider {
    private OkHttpClient client = new OkHttpClient();

    /**
     * 获取AccessToken
     */
    public String getAccessToken(AccessTokenDAO accessTokenDto) {
        MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(accessTokenDto));

        Request request = new Request.Builder()
                .url("https://gitee.com/oauth/token?grant_type=authorization_code")
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            String str = response.body().string();
            return JSONObject.parseObject(str).getString("access_token");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据access_token获取用户信息
     */
    public GiteeUser getGiteeUser(String access_token) {
        Request request = new Request.Builder()
                .url("https://gitee.com/api/v5/user?access_token="+access_token)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return JSON.parseObject(response.body().string(), GiteeUser.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
